/**
 * 
 */
package com.wolfpire.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.dao.RoleAuthorityRelationDao;
import com.wolfpire.system.dao.RoleDao;
import com.wolfpire.system.dao.UserGroupDao;
import com.wolfpire.system.dao.UserGroupUserRelationDao;
import com.wolfpire.system.dao.UserRoleRelationDao;
import com.wolfpire.system.model.Role;
import com.wolfpire.system.model.RoleAuthorityRelation;
import com.wolfpire.system.model.UserGroup;
import com.wolfpire.system.service.RoleService;
import com.wolfpire.system.service.UserGroupService;

/**
 * @author lihd
 *
 */

@Service("userGroupService")
public class UserGroupServiceImpl extends BaseHibernateService<UserGroup, Long> implements
	UserGroupService {
	
	@Resource private UserGroupDao userGroupDao;
	@Resource private UserGroupUserRelationDao userGroupUserRelationDao;
	

	public List<UserGroup> list(UserGroup entity) {
		return this.userGroupDao.list(entity);
	}

	public Page<UserGroup> setPageDataList(Page<UserGroup> page,
			UserGroup filterUserGroup) {
		return this.userGroupDao.findUserGroups(page, filterUserGroup);
	}

	public boolean isUnique(UserGroup userGroup) {
		return this.isPropertyUnique("name", userGroup.getName(), userGroup.getId());
	}
	
	public void saveOrUpdate(UserGroup userGroup) {
		super.saveOrUpdate(userGroup);
		/*Long id = userGroup.getId();
		Long[] authorityIds = role.getAuthorityIds();
		this.roleAuthorityRelationDao.relate(id, authorityIds);*/
	}

	@Override
	protected IBaseHibernateDao<UserGroup, Long> getEntityDao() {
		// TODO Auto-generated method stub
		return this.userGroupDao;
	}

	public void saveUserGroupUser(Long id, String userIdsStr) {
		List<Long> userIds = new ArrayList<Long>();
		if (!StringUtils.isBlank(userIdsStr)) {
			String[] userIdsArr = userIdsStr.split(",");
			for (String str : userIdsArr) {
				userIds.add(Long.parseLong(str));
			}
		}
		this.userGroupUserRelationDao.relate(id, userIds);
		
	}

	public List<UserGroup> findUserGroupByUserId(long userId) {
		return this.userGroupDao.findUserGroupByUserId(userId);
	}

	
	
}
